package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * */
    @Override
    public void addCart(ShoppingCartDTO dto) {
        // 创建shoppingcart对象
        ShoppingCart cart = new ShoppingCart();
        // 拷贝属性值
        BeanUtils.copyProperties(dto, cart); // 浅拷贝
        // 设置用户id
        cart.setUserId(BaseContext.getCurrentId());
        // 判断该商品是否以及存在购物车，根据菜品id，用户id，口味判断
        ShoppingCart cartInDB = shoppingCartMapper.getBys(cart);
        if (cartInDB == null) {
            // 数据库没有改商品数据
            // 补充缺失的属性值
            if (dto.getDishId()!=null){
                // 根据菜品id查询
                Dish dish = dishMapper.getById(dto.getDishId());
                cart.setName(dish.getName());
                cart.setAmount(dish.getPrice());
                cart.setImage(dish.getImage());
                cart.setNumber(1);
                cart.setCreateTime(LocalDateTime.now());
            }else{
                // 新增套餐
                SetmealVO setmealVO = setmealMapper.getById(dto.getSetmealId());
                cart.setName(setmealVO.getName());
                cart.setAmount(setmealVO.getPrice());
                cart.setImage(setmealVO.getImage());
                cart.setNumber(1);
                cart.setCreateTime(LocalDateTime.now());
            }
            // 存入数据库
            shoppingCartMapper.add(cart);
        }else {
            // 购物车有该商品数据，直接+1
            cartInDB.setNumber(cartInDB.getNumber()+1);
            shoppingCartMapper.updateNumber(cartInDB);
        }

    }
    /**
     * 查询购物车列表
     * */
    @Override
    public List<ShoppingCart> list() {

        return shoppingCartMapper.getByUserId(BaseContext.getCurrentId());
    }
    /**
     * 清空购物车
     * */
    @Override
    public void cleanCart() {
        shoppingCartMapper.cleanCart(BaseContext.getCurrentId());
    }
    /**
     * 删除购物车中的一个商品
     * */
    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(dto, cart);
        cart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cartInDB = shoppingCartMapper.getBys(cart);
        if (cartInDB.getNumber()>1) {
            cartInDB.setNumber(cartInDB.getNumber()-1);
            shoppingCartMapper.updateNumber(cartInDB);
        }else {
            shoppingCartMapper.delete(cartInDB.getId());
        }
    }
}
